package com.runen.infinitemovement.tool;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.model.LatLng;

/**
 * Created by Administrator on 2019/8/28.
 * 定位服务类
 */

public class LocationUtil {

    private volatile static LocationUtil locationUtil; // 本类实例

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;

    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;

    //声明定位回调监听器
    AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                loc = new LatLng(amapLocation.getLatitude(),amapLocation.getLongitude());
                if (amapLocation.getErrorCode() == 0) {
                    listener.getLocation(amapLocation);
                } else {
                    Log.e("AmapError", "location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());
                    listener.locationFail();
                }
            }

        }
    };
    private LatLng loc;

    /**
     * 获取 LocationUtil 的实例
     */
    public static LocationUtil getInstance() {
        if (locationUtil == null) {
            synchronized (LocationUtil.class) {
                if (locationUtil == null) {
                    locationUtil = new LocationUtil();
                }
            }
        }
        return locationUtil;
    }

    /**
     * 定位初始化
     *
     * @param context
     */
    public void initOption(Context context, boolean once) {
        //初始化定位
        mLocationClient = new AMapLocationClient(context);
        //设置定位回调监听
        mLocationClient.setLocationListener(mAMapLocationListener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //关闭缓存机制
//        mLocationOption.setLocationCacheEnable(false);
        mLocationOption.setNeedAddress(true);//需要返回地址
//        mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
        mLocationOption.setInterval(3000);//连续定位间隔时间
        mLocationOption.setOnceLocation(once);//设置是否单次定位,默认false
        mLocationClient.setLocationOption(mLocationOption);
    }

    /**
     * 开启服务
     */
    public void startLocation() {
        if (mLocationClient != null)
            mLocationClient.startLocation();
    }


    /**
     * 关闭服务
     */
    public void stopLocation() {
        if (mLocationClient != null) {
            mLocationClient.disableBackgroundLocation(true);
            mLocationClient.stopLocation();
            mLocationClient.unRegisterLocationListener(mAMapLocationListener);
            mLocationClient.onDestroy();
        }
        mLocationClient = null;
        mLocationOption = null;

    }

    /**
     * 获取当前位置
     *
     * @return
     */
    public LatLng getCurrentLoc() {
        if (loc != null)
            return loc;
        return null;
    }

    public interface GetLocationSuccseeListener {
        void getLocation(AMapLocation latLng);

        void locationFail();
    }

    private static GetLocationSuccseeListener listener;

    public void setLocationListener(GetLocationSuccseeListener listener) {
        this.listener = listener;
    }
}
